articles

Home / DeveloperSection / Articles / Class Concepts in Java: Parameterized and Default Constructors (Part-5)

Class Concepts in Java: Parameterized and Default Constructors (Part-5)

Simond Gear1808 17-May-2016

After the object is constructed, we may still call its setter methods (provided we defined them in the class definition) to change the value of data members to any other value. By providing a no-argument constructor, we ensure that any newly created object will have its data members set to the default values specified in the constructor.


Now, what if we want to set the data members to a different value each time we create an object? For this, we need to pass the values as parameters to the constructor. Therefore, we need to declare a constructor that takes parameters. This type of constructors is known as parameterized constructor, such a constructor is shown in the following code fragment:

public Point(int x, int y)
 {
     this.x = x;
     this.y = y;
}

 We assign the values specified by the two parameters to the two instance variables using this as a reference to the current object. To call this constructor in our program code, we use the following statement:

Point p = new Point(4, 5);

When we call the constructor, we set the values for its arguments. The instance variables will now be initialized using these values. We can verify this by dumping the object’s contents as illustrated in the previous example.

Sometimes, we may want to initialize only one of the instance variables while letting the other variable have some default value. In such a case, we declare another constructor for our Point class, as follows:

public Point(int x)
{
   this.x = x;
    y = 10;
}

 

This constructor takes only one argument, the value of which is assigned to the x field. The other instance variable, y, is set to a value of 10. If we do not assign a value to the variable y, it will take a default value of 0, which is provided by the compiler. We can now call the constructor with the following statement:

Point p = new Point(5);

This creates the Point object (5, 10), which can be verified by printing the object’s contents.

A class may declare more than one constructor in its definition. If this is the case, how do we know which constructor is called when the class is instantiated? The compiler decides which constructor to call depending on the number of parameters and their types. Look at the following code fragment:

Point p1 = new Point();

Point p2 = new Point(15);

Point p3 = new Point(5, 10);

·     The first statement calls the no-argument constructor and creates the object p1(10, 10).

·     The second statement creates the object p2(15, 10),

·      and the third statement creates the object p3(5, 10).

Thus, the compiler has decided which constructor to call depending on the number of parameters passed to it.

There is one more noticeable thing here is that, we cannot write two constructors that have the same number and type of arguments for the same class because the platform would not be able to differentiate between them. Doing so causes a compile-time error.

Default Constructor

In the previous section, we learned to write our own constructor. In all our previous examples, we hadn’t written any constructors. So is there any constructor provided by default? Yes, there is. If we do not write a constructor, the Java compiler provides a constructor with no arguments. This is called the default constructor, and the implementation of the default constructor is null, which means it does not contain any code.

Rules for Defining a Constructor
The rules for constructor creation can be summarized as follows:

·         A constructor must have the same name as the class name.

·         A constructor does not return anything, not even a void type.

·         A constructor may take zero or more arguments.

·         By default, the compiler provides a no-argument constructor.


Updated 16-Dec-2017

Leave Comment

Comments

Liked By